package kamikaze.service.product;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import kamikaze.entity.Product;
import kamikaze.support.HttpMethod;
import kamikaze.support.Service;
import kamikaze.support.ServiceConfig;
import kamikaze.support.ServiceContext;

@ServiceConfig(path = "product/browse", method = HttpMethod.GET)
public class BrowseProductService implements Service {

	@SuppressWarnings("unchecked")
	@Override
	public String serve(ServiceContext ctx) throws Exception {
		ctx.getUserSession().ensureLoginAsUser();
		List<Product> productList = new ArrayList<Product>();

		Query q = ctx
				.getEntityManager()
				.createQuery(
						"Select p from Product p where deleted=false");
		productList = q.getResultList();

		ctx.getRequest().setAttribute("products", productList);
		return "product/browse";

	}

}
